package com.gdzn.signal.api.inner;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.gdzn.common.core.utils.JsonUtil;
import com.gdzn.signal.mongo.document.*;
import com.gdzn.signal.service.IDevCabinetService;
import com.gdzn.signal.service.impl.SignalServiceImpl;
import com.gdzn.common.core.domain.R;
import com.gdzn.common.core.utils.GsonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 数据采集api
 */
@RestController
@RequestMapping("/inner/signal" )
public class SignalApiController {

    @Autowired
    private SignalServiceImpl signalServiceImpl;

    @Autowired
    private IDevCabinetService devCabinetService;


    /**
     * 保存上报信号
     * @param jsonStr
     * @return
     */
    @PostMapping("/jsonStr")
    public R add2(@RequestBody String jsonStr) {
        Map paramMap = JsonUtil.toMap(jsonStr);
        if("110".equals(paramMap.get("msgType").toString()) || "111".equals(paramMap.get("msgType").toString())){
            MgSignalLogin signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalLogin(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("310".equals(paramMap.get("msgType").toString()) || "311".equals(paramMap.get("msgType").toString())){
            MgSignalAttribute signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalAttribute(),false);
            signalServiceImpl.saveSignal(signal);
            devCabinetService.saveOrUpdateCabinetInfo(jsonStr);
        }
        if("410".equals(paramMap.get("msgType").toString()) || "411".equals(paramMap.get("msgType").toString())){
            MgSignalAlarm signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalAlarm(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("500".equals(paramMap.get("msgType").toString()) || "501".equals(paramMap.get("msgType").toString())){
            MgSignalCmdControl signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalCmdControl(),false);
            signalServiceImpl.saveSignal(signal);
        }
        if("210".equals(paramMap.get("msgType").toString()) || "211".equals(paramMap.get("msgType").toString())){
            MgSignalCmdQconfig signal = BeanUtil.fillBeanWithMap(paramMap,new MgSignalCmdQconfig(),false);
            signalServiceImpl.saveSignal(signal);
        }
        return R.ok();
    }

    /**
     * 查询最新上报属性
     * @param devId
     * @return
     */
    @GetMapping("/attribute/{devId}")
    public R attribute(@PathVariable("devId" ) String devId) {
        MgSignalAttribute signal = signalServiceImpl.getAttribute(devId);
        return R.ok(signal);
    }

}
